export class DB {
	constructor(arg) {
		this.isopenDb = this.checkDb();
		this.openDb();
	}
	/**
	 * 检查数据库是否 存在
	 */
	checkDb() {
		return plus.sqlite.isOpenDatabase({
			name: "data",
			path: "_doc/data.db"
		});
	}
	/**
	 * 打开数据库
	 */
	openDb() {
		if (this.checkDb()) {return false};
		plus.sqlite.openDatabase({
			name: 'data',
			path: '_doc/data.db',
			success: (e) => {
				console.log('openDatabase success!');
			},
			fail: function(e) {
				console.log('openDatabase failed: ' + JSON.stringify(e));
			}
		});
	}
	/**
	 * 执行增删改等操作
	 */
	executeSql(...dat){
		return new Promise((resolve,reject)=>{
			plus.sqlite.executeSql({
				name:"data",
				sql:dat[0],
				success: (e) => {
					resolve(e);
				},
				fail: (e) => {
					reject(e);
				}
			})
			
		})
	}
	/**
	 * 查询语言
	 * @param {Object} language_type
	 */
	select(...dat) {
		return new Promise((resolve, reject) => {
			plus.sqlite.selectSql({
				name: 'data',
				sql: dat[0],
				success: function(res) {
					resolve(res);
				},
				fail: function(e) {
					reject(e);
				}
			});
		})

	}
	/**
	 * 将下载任务的唯一标识id 存储到res_list 的backID中
	 */
	changeBackID(...arg) {
		plus.sqlite.executeSql({
			name: "data",
			sql: "update res_list set backId ='" + backId + "',downStatus ='" + downStatus +
				"'  where (id = " + id + ")",
			success: function() {
				console.log("下载任务标识记录成功 backID " + backId + " id +" + id + " downStatus " + downStatus);
			},
			fail: function(e) {
				console.log("下载任务标识记录失败" + JSON.stringify(e));
			}
		})
	}
	
	/**
	 * 关闭数据库
	 */
	closeDb(){
		
		 plus.sqlite.closeDatabase({
			 name:"data",
			 path: "_doc/data.db"
		 });
		
		
	}





}
